#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n, s;
ll c[5], d[5], e[5];
ll dp[N];
void init() {
  dp[0] = 1;
  rep(i, 1, 4) {
    rep(j, c[i], 100000) { dp[j] += dp[j - c[i]]; }
  }
}
ll solve() {
  ll res = 0;
  rep(i, 1, 4) e[i] = c[i] * (d[i] + 1);
  rep(i, 0, 1) {
    rep(j, 0, 1) {
      rep(k, 0, 1) {
        rep(u, 0, 1) {
          ll f = (i + j + k + u) % 2 ? -1 : 1;
          ll sum = i * e[1] + j * e[2] + k * e[3] + u * e[4];
          if (sum <= s) res += f * dp[s - sum];
        }
      }
    }
  }
  return res;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  rep(i, 1, 4) cin >> c[i];
  init();
  int t;
  cin >> t;
  while (t--) {
    rep(i, 1, 4) cin >> d[i];
    cin >> s;
    cout << solve() << endl;
  }
  return 0;
}